package com.kongwg.exer;

import org.junit.Test;

/**
 * @author kongwg
 * @create 2020-12-08 10:18
 */
public class StringDemo1 {
    /**
     * 获取一个字符串在另一个字符串中出现的次数。
     *       比如：获取“ ab”在 “abkkcadkabkebfkabkskab” 中出现的次数
     */
    public int getCount(String mainStr,String subStr){
        int count =0;
        int index=0;
        if(mainStr != null){
//            while ((index = mainStr.indexOf(subStr)) !=-1){
//                count++;
//                mainStr = mainStr.substring(index + subStr.length());
//
//            }
            while ((index = mainStr.indexOf(subStr,index)) != -1){
                count++;
                index += subStr.length();
            }
            return count;
        }
        return count;
    }

    @Test
    public void test1(){
    StringDemo1 st = new StringDemo1();
    String s = "abkkcadkabkebfkabkskab";
    String sb = "ab";
        int ab = st.getCount(s, sb);

        System.out.println(ab);
    }

}
